<?php
if(!defined('IN_ADAGPS'))
{
    die(SYSTEM_ACCESS_DENIED);
}

    $objXajax->registerFunction('frmchartgeneral_viewchart'); 
    $arrTitle = array(
        1=>'Doanh thu theo ngày',
        2=>'Doanh thu theo phòng',
        3=>'Theo người thu',
        4=>'Thu nhập trung bình theo ngày trong tuần',
        5=>'Tiền chi'
    );
    function frmchartgeneral_viewchart($charttype=1,$dateFrom='',$dateTo=''){
    
        global $objDbSelect,$arrTitle;
        $objResponse = new xajaxResponse();
        include(ROOT_PATH.'adagps.com/Charts/ofc/php-ofc-library/open-flash-chart.php');    
        $dateFrom = $dateFrom==''?SYSTEM_DATE_START  : $dateFrom;
        $dateTo = $dateTo==''?SYSTEM_DATE  : $dateTo;
        $arrResult = frmchartgeneral_createArray($charttype,$dateFrom,$dateTo);
        
        $arrName = Array(); 
        $max = 5;
        $title = new title( $arrTitle[$charttype],20) ;
        $format = 2;
        $sName='';
        $max =0;
        $min =0;
        $chart = new open_flash_chart();
        $barLabel = array();
        $barArr = array();
        $bar =0;
        
        if($charttype==1){
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['timeend'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int)$arrResult[$i]['totalprice'];
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;
                $d = new dot($dec);               
                $barArr[] = $d->colour('#D02020')->tooltip($sName."<br>".$dec);  
               
              }
              $bar = new line();
              $bar->set_tooltip('Value = #val');
              $bar->set_on_show(new line_on_show('grow-up', 2.5, 0));
              $bar->set_values($barArr); 
              
             
        }else if($charttype==2){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['roomname'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int) $arrResult[$i]['totalprice'];
                $barArr[] = $dec; 
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;

            }
           // die();
            $bar = new bar_glass();
            $bar->set_colour('#68D5ED');
            $bar->set_values($barArr);
            $bar->key($arrTitle[$charttype],12); 
            
        }else if($charttype==3){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  $arrResult[$i]['username'];
                $xx = new x_axis_label($sName, '#050069', 13, 340);            
                $barLabel[] = $xx;
                $dec = (int)$arrResult[$i]['totalprice'];
                $barArr[] = $dec;
                if($dec>$max)$max=$dec;
                if($dec<$min)$min=$dec;  

            }
            $bar = new bar_glass();
            $bar->set_colour('#68D5ED');
            $bar->set_values($barArr);
        }
        else if($charttype==4){
            $barArr = array();
            for($i=0;$i<count($arrResult);$i++){
                $sName =  convertToNameDayOfWeek($arrResult[$i]['dayweek']);
                if($arrResult[$i]['numdw']!=0){
                  $dec = (int)$arrResult[$i]['totalprice']/$arrResult[$i]['numdw'];  
                }                
                 array_push($barArr,new pie_value($dec,addslashes("$sName")));

            }
            $bar = new pie();  
            $bar->set_alpha(0.8);
            $bar->set_start_angle( 35 );
            $bar->add_animation( new pie_fade() )->add_animation( new pie_bounce(10) ) ; 
            $bar->set_tooltip( '#val# VNĐ <br> #percent# ' );
            $bar->set_colours(array('#1C9E05','#FF368D','#0000FF','#06DCFB','#FF800D','#B96F6F','#06DCFB'));
            $bar->set_values( $barArr );

        }
        if($charttype!=4){
            $rangeMax =0;
            $rangeMin =0;
            $rangStep=1;
            createRangeForChart($min,$max,$rangeMax,$rangeMin);
            $y = new y_axis();
            $y->set_range($rangeMin,$rangeMax);
            $y->set_steps(($rangeMax-$rangeMin)/5);
            
            $x_labels = new x_axis_labels();
            $x = new x_axis();
            $x_labels->set_labels($barLabel );
            $x->set_labels($x_labels);
               
            $chart->set_x_axis($x);
            $chart->set_y_axis($y);
            
        }
        
        

        $chart->add_element($bar);
        $chart->set_title($title);        
        $values = $chart->toPrettyString();
        
       $sScript = 'data_chartgeneral=' . $values .';';  
       $objResponse->addScript($sScript);    
        $sScript1 = 'swfobject.embedSWF("Charts/ofc/open-flash-chart.swf", "frmchartgeneral_divGrid", "60%", "400", "9.0.0", "expressInstall.swf", {"get-data":"open_flash_chart_data_chartgeneral"}, {"wmode":"opaque"});';
        $objResponse->addScript($sScript1);  
        $objResponse->addScript("changeWaiting(1);");  
                          
        return $objResponse->getXML();    
    }
    
function frmchartgeneral_createArray($charttype,$dateFrom,$dateTo){
    global $objDbUpdate;
    $hotel_id = $_SESSION['current']['hotel_id'];
    $tbl_postfix = $_SESSION['current']['tbl_postfix'];
   
    $groupby = " group by DATE_FORMAT(h.timeend,'%Y-%m-%d') order by  DATE_FORMAT(h.timeend,'%Y-%m-%d') ";
    if($charttype==2){
     $groupby = " group by h.room_id order by   r.name";
    }
    elseif($charttype==3){
      $groupby = ' group by h.user_update order by h.user_update ' ; 
    } elseif($charttype==4){
      $groupby = ' group by DAYOFWEEK(h.timeend) order by DAYOFWEEK(h.timeend) ' ; 
    }
    
    $sql = " select r.name as roomname,DATE_FORMAT(h.timeend,'%Y-%m-%d') as timeend,
     u.username as username,sum(h.price) as price,sum(h.totalprice) as totalprice ,  DAYOFWEEK(h.timeend) as dayweek,count(DAYOFWEEK(h.timeend)) as numdw
     from tbl_history_book_room$tbl_postfix as h join tbl_room r ON h.room_id = r.id join tbl_user as u ON u.id = h.user_update 
     where h.hotel_id = $hotel_id and datediff('$dateFrom',h.timeend)<=0 and datediff('$dateTo',h.timeend)>=0 $groupby ";//
     //echo $sql; die();
     $arr = $objDbUpdate->GetArray($sql);
     $count = count($arr);
     for($i=0;$i<$count;$i++){
          $arr[$i]['productprice']= $arr[$i]['totalprice'] - $arr[$i]['price'] ; 
     } 
     return  $arr  ;
} 


   
?>